using System.IO;
using Microsoft.Extensions.FileProviders;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

// 允许API跨域请求（用于开发环境）
app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());

// 配置静态文件中间件以从UI文件夹提供文件
var staticFileOptions = new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), "UI")),
    RequestPath = ""
};

// 配置默认文件中间件以从UI文件夹提供默认文件
var defaultFilesOptions = new DefaultFilesOptions();
defaultFilesOptions.DefaultFileNames.Clear();
defaultFilesOptions.DefaultFileNames.Add("index.html");
defaultFilesOptions.FileProvider = new PhysicalFileProvider(
    Path.Combine(Directory.GetCurrentDirectory(), "UI"));

// 使用默认文件中间件（必须在UseStaticFiles之前）
app.UseDefaultFiles(defaultFilesOptions);
// 使用静态文件中间件
app.UseStaticFiles(staticFileOptions);

app.UseAuthorization();

app.MapControllers();

app.Run();
